﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using NTTDATA.BEENChina.ResultCertification.model;

namespace NTTDATA.BEENChina.ResultCertification.DataStore
{
    public class ProcessLogAccess:IProcessLog
    {

        private static string ConnectionString
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["ResultCertificationDB"];
            }
        }

        public int Write(string message, string model)
        {
            int effactRow = 0;
            string sql = " INSERT INTO  [ProcessLog] " +
                         "      (  [Message] ,[Model] ,[UpdateTime] )   " +
                         "  VALUES ( @Message ,@Model ,@UpdateTime )    ";

            SqlParameter[] sqlParameters = 
                {
                    new SqlParameter("@Message",  message),
                    new SqlParameter("@Model",  model),
                    new SqlParameter("@UpdateTime",  System.DateTime.Now),

                };
            try
            {
                effactRow = SqlHelper.ExecuteNonQuery(ConnectionString, System.Data.CommandType.Text, sql, sqlParameters);
            }
            catch (Exception ex)
            {
                Log.Console.Error("ProcessLogAccess.Write", ex);
            }

            return effactRow;
        }

        public List<ProcessLogItem> GetProcessLogs(string model)
        {
            List < ProcessLogItem > processLogItems = null;
            string sql = "  SELECT TOP 50 [ID] ,[Message],[Model],[UpdateTime]  " +
                         "  FROM [ProcessLog] WITH (NOLOCK)  " +
                         "  WHERE [Model] = @Model" +
                         "  ORDER BY [ID] DESC ";

            
            SqlParameter[] sqlParameters = 
                {
                    new SqlParameter("@Model",  model)
                };
            try
            {
                using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, System.Data.CommandType.Text, sql, sqlParameters))
                {
                    while (reader.Read())
                    {


                        var item = new ProcessLogItem
                            {
                                Id = reader["ID"].ToString(),
                                Message = reader["Message"].ToString(),
                                ProcessModel = reader["Model"].ToString(),
                                UpdateTime = reader["UpdateTime"].ToString()
                            };

                        if (processLogItems == null)
                            processLogItems = new List<ProcessLogItem>();
                        processLogItems.Add(item);
                    }

                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Console.Error("ProcessLogAccess.GetProcessLogs", ex);
            }

            return processLogItems;
        }

        public List<ProcessLogItem> GetProcessLogs()
        {
            List<ProcessLogItem> processLogItems = null;
            string sql = "  SELECT TOP 50 [ID] ,[Message],[Model],[UpdateTime]  " +
                         "  FROM [ProcessLog] WITH (NOLOCK)  " +
                         "  WHERE [Model] <>  '結果統計' " +
                         "  ORDER BY [ID] DESC ";

 
            try
            {
                using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, System.Data.CommandType.Text, sql))
                {
                    while (reader.Read())
                    { 

                        var item = new ProcessLogItem
                        {
                            Id = reader["ID"].ToString(),
                            Message = reader["Message"].ToString(),
                            ProcessModel = reader["Model"].ToString(),
                            UpdateTime = reader["UpdateTime"].ToString()
                        };

                        if (processLogItems == null)
                            processLogItems = new List<ProcessLogItem>();
                        processLogItems.Add(item);
                    }

                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Console.Error("ProcessLogAccess.GetProcessLogs", ex);
            }

            return processLogItems;
        }

        public List<ProcessLogItem> GetMaxProcessLogs(string maxId)
        {
            List<ProcessLogItem> processLogItems = null;
            string sql = "  SELECT TOP 50 [ID] ,[Message],[Model],[UpdateTime]  " +
                         "  FROM [ProcessLog] WITH (NOLOCK)  " +
                         "  WHERE ID > @Id  AND   [Model] <>  '結果統計' " +
                         "  ORDER BY [ID] DESC ";

            SqlParameter[] sqlParameters = 
                {
                    new SqlParameter("@Id", SqlDbType.BigInt)
                };

            sqlParameters[0].Value = Convert.ToInt64(maxId);

            try
            {
                using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, System.Data.CommandType.Text, sql, sqlParameters))
                {
                    while (reader.Read())
                    {

                        var item = new ProcessLogItem
                        {
                            Id = reader["ID"].ToString(),
                            Message = reader["Message"].ToString(),
                            ProcessModel = reader["Model"].ToString(),
                            UpdateTime = reader["UpdateTime"].ToString()
                        };

                        if (processLogItems == null)
                            processLogItems = new List<ProcessLogItem>();
                        processLogItems.Add(item);
                    }

                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Console.Error("ProcessLogAccess.GetMaxProcessLogs", ex);
            }

            return processLogItems;
        }
    }
}
